package com.jiniutech.enterprise.controller;

import com.jiniutech.common.result.ResultWrap;
import com.jiniutech.common.util.wx.MessageUtil;
import com.jiniutech.common.util.wx.entity.message.BaseMessage;
import com.jiniutech.enterprise.component.CommunicateConfig;
import com.jiniutech.enterprise.component.EnterpriseConfig;
import com.jiniutech.enterprise.util.aes.AesException;
import com.jiniutech.enterprise.util.aes.WXBizMsgCrypt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.annotation.Resource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;

/**
 * 用于测试企业微信的controller
 * 企业微信用户端发送消息过来的时候需要通过get方法验证请求有效性，
 * 验证成功后带消息发送post调用到服务器
 * 服务端获得密文之后需要对密文解密得到消息，然后对消息进行处理
 */
@RestController
@RequestMapping(value = "/test")
@Slf4j
public class TestController extends ParentController{

    @Resource
    private EnterpriseConfig config;

    @Resource
    private CommunicateConfig communicateConfig;

    @GetMapping("/testOne")
    @ResultWrap(wrap = false)
    public String testOne(@RequestParam(name = "msg_signature") String msgSignature,
                          @RequestParam Integer timestamp,
                          @RequestParam String nonce,
                          @RequestParam(name = "echostr") String echoStr) {
        return super.getCheckResult(msgSignature,timestamp,nonce,echoStr,config);
    }

    @PostMapping("/testOne")
    @ResultWrap(wrap = false)
    public String testOne(@RequestParam(name = "msg_signature") String msgSignature,
                          @RequestParam String nonce,
                          @RequestParam Integer timestamp,
                          @RequestBody String echoStr) {
        String sToken = config.getToken();
        String sCorpID = config.getCropId();
        String sEncodingAESKey = config.getEncodingAESKey();

        try {
            WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID);
            String sMsg = wxcpt.DecryptMsg(msgSignature, timestamp.toString(), nonce, echoStr);
            System.out.println("after decrypt msg: " + sMsg);
            BaseMessage message = MessageUtil.getMessage(sMsg);
            System.out.println(message.getMsgType());

        } catch (Exception e) {
            // TODO
            // 解密失败，失败原因请查看异常
            e.printStackTrace();
        }
        return "123";
    }

    @GetMapping("/testCommunicate")
    @ResultWrap(wrap = false)
    public String testCommunicate(@RequestParam(name = "msg_signature") String msgSignature,
                          @RequestParam Integer timestamp,
                          @RequestParam String nonce,
                          @RequestParam(name = "echostr") String echoStr) {
        return super.getCheckResult(msgSignature,timestamp,nonce,echoStr,communicateConfig);
    }
}
